home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / program / misc / bgui12.lha / demos / WallStreet / Timer.c < prev    next >
C/C++ Source or Header  |  1995-05-16  |  5KB  |  170 lines

  1. /*
  2.  *      TIMER.C
  3.  */
  4.  
  5. #include "WallStreet.h"
  6.  
  7. /*
  8.  *      Export symbols.
  9.  */
  10. Prototype BOOL SetupTimer( void );
  11. Prototype VOID KillTimer( void );
  12. Prototype VOID TriggerTimer( ULONG secs );
  13. Prototype VOID StopTimer( void );
  14. Prototype BOOL CheckTimer( void );
  15. Prototype ULONG TimerMask;
  16.  
  17. struct MsgPort          *TimerPort = NULL;
  18. ULONG                    TimerMask = 0L;
  19. struct IORequest        *TimerReq = NULL;
  20.  
  21. /*
  22.  *      Some macros.
  23.  */
  24. #define SET_COMMAND(r,c)          (( struct timerequest * )r )->tr_node.io_Command = c
  25. #define SET_SECONDS(r,s)          (( struct timerequest * )r )->tr_time.tv_secs    = s
  26. #define SET_MICROS(r,m)           (( struct timerequest * )r )->tr_time.tv_micro   = m
  27. #define GET_ERROR(r)              (( struct timerequest * )r )->tr_node.io_Error
  28.  
  29. /*
  30.  *      Open up the timer.device.
  31.  */
  32. BOOL SetupTimer( void )
  33. {
  34.         /*
  35.          *      Create a port.
  36.          */
  37.         if ( TimerPort = CreateMsgPort()) {
  38.                 /*
  39.                  *      Create timer request block.
  40.                  */
  41.                 if ( TimerReq = CreateIORequest( TimerPort, sizeof( struct timerequest ))) {
  42.                         /*
  43.                          *      Open the device.
  44.                          */
  45.                         if ( ! OpenDevice( TIMERNAME, UNIT_VBLANK, TimerReq, 0L )) {
  46.                                 /*
  47.                                  *      Set global port mask.
  48.                                  */
  49.                                 TimerMask = ( 1L << TimerPort->mp_SigBit );
  50.                                 /*
  51.                                  *      Initialize it.
  52.                                  */
  53.                                 SET_COMMAND( TimerReq, TR_ADDREQUEST );
  54.                                 return( TRUE );
  55.                         }
  56.                         DeleteIORequest( TimerReq );
  57.                         TimerReq = NULL;
  58.                 }
  59.                 DeleteMsgPort( TimerPort );
  60.                 TimerPort = NULL;
  61.         }
  62.         return( FALSE );
  63. }
  64.  
  65. /*
  66.  *      Close up the timer.device.
  67.  */
  68. VOID KillTimer( void )
  69. {
  70.         /*
  71.          *      All OK?
  72.          */
  73.         if ( TimerReq ) {
  74.                 /*
  75.                  *      Request pending?
  76.                  */
  77.                 if ( ! CheckIO( TimerReq )) {
  78.                         /*
  79.                          *      Abort.
  80.                          */
  81.                         AbortIO( TimerReq );
  82.                         /*
  83.                          *      Pop aborted request.
  84.                          */
  85.                         WaitIO( TimerReq );
  86.                 }
  87.                 /*
  88.                  *      Close device.
  89.                  */
  90.                 CloseDevice( TimerReq );
  91.                 /*
  92.                  *      Delete request.
  93.                  */
  94.                 DeleteIORequest( TimerReq );
  95.                 /*
  96.                  *      Delete port.
  97.                  */
  98.                 DeleteMsgPort( TimerPort );
  99.         }
  100. }
  101.  
  102. /*
  103.  *      Trigger a timer request.
  104.  */
  105. VOID TriggerTimer( ULONG secs )
  106. {
  107.         /*
  108.          *      All OK?
  109.          */
  110.         if ( TimerReq ) {
  111.                 /*
  112.                  *      Request pending?
  113.                  */
  114.                 if ( ! CheckIO( TimerReq )) {
  115.                         /*
  116.                          *      Abort.
  117.                          */
  118.                         AbortIO( TimerReq );
  119.                         /*
  120.                          *      Pop aborted request.
  121.                          */
  122.                         WaitIO( TimerReq );
  123.                 }
  124.                 /*
  125.                  *      Setup the time-delay.
  126.                  */
  127.                 SET_MICROS( TimerReq, 0L );
  128.                 SET_SECONDS( TimerReq, secs );
  129.                 /*
  130.                  *      Send the request.
  131.                  */
  132.                 SendIO( TimerReq );
  133.         }
  134. }
  135.  
  136. /*
  137.  *      Stop the timer.
  138.  */
  139. VOID StopTimer( void )
  140. {
  141.         /*
  142.          *      All OK?
  143.          */
  144.         if ( TimerReq ) {
  145.                 /*
  146.                  *      Request pending?
  147.                  */
  148.                 if ( ! CheckIO( TimerReq )) {
  149.                         /*
  150.                          *      Abort it.
  151.                          */
  152.                         AbortIO( TimerReq );
  153.                         /*
  154.                          *      Pop request.
  155.                          */
  156.                         WaitIO( TimerReq );
  157.                 }
  158.         }
  159. }
  160.  
  161. /*
  162.  *      Check if it's a valid timer message.
  163.  */
  164. BOOL CheckTimer( void )
  165. {
  166.         BOOL                    rc = TRUE;
  167.  
  168.         return( GET_ERROR( TimerReq ) ? FALSE : TRUE );
  169. }
  170.